// 给数字 num 前补足0到 length 位数
export const appendZero = (num, length) => {
    num = (num || 0).toString()
    if (num.length >= length) return num
    for (let i = length - num.length; i > 0; i--) num = '0' + num
    return num
}

// 把数字 num 转化为 '周X'，例如 1 转化为 '周一'
export const getWeekdayName = (num) => {
    num = parseInt(num) || 0
    if (num < 1 || num > 7) return ''
    let arr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    return arr[num - 1]
}

// 向上取整，返回一个0-10之间的随机整数
export const getIndex = () => {
  return Math.ceil(Math.random() * 10);
}

// 当前数字转千分位
export const toThousands = (num) => {
  num = (num || 0).toString()
  let number = 0
  let floatNum = ''
  let intNum = ''
  // 判断是否有小数位，有则截取小数点后的数字
  if (num.indexOf('.') > 0) {
    number = num.indexOf('.') // 获取小数点出现的位置
    floatNum = num.substr(number) // 截取arr.substr(form, length)
    intNum = num.substring(0, number) // 截取arr.substring(start, end)
  } else {
    intNum = num
  }
  let result = []
  let counter = 0
  intNum = intNum.split('')
  // 利用3的倍数，向数组插入','
  for (let i = intNum.length - 1; i >= 0; i--) {
    counter++
    result.unshift(intNum[i])
    if (!(counter % 3) && i !== 0) {
      result.unshift(',')
    }
  }
  return result.join('') + floatNum || ''
}

// 四位随机数
export const rand = (min,max) =>{
  return Math.floor(Math.random()*(max-min)) + min;
}

// ID编码
export const toHex = (num) => {
  var rs = "";
  var temp;
  while(num/16 > 0){
    temp = num%16;
    rs = (temp+"").replace("10","a").replace("11","b").replace("12","c").replace("13","d").replace("14","e").replace("15","f") + rs;
    num = parseInt(num/16);
  }
  //转大写
  return '3ST' + rs.toLocaleUpperCase().padStart(13,"0");
}
export const randNum4 = () => {
  return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
}

/**
 * @function getSubStr
 * @param {string} str 截取前字符串
 * @param {number} len 截取长度,中文字符算2长度
 * @param {boolean} isSuffix 是否加省略号，默认不加
 * @return {string} 截取后字符串
 * @description 截取字符串,中文字符算2长度
 * @example var str = getSubStr("test测试",6);普通截取
 * @example var str = getSubStr("test测试",6,true); 截取后面加省略号
 */
const getStrRealLen = (str) =>{/*获取字符串真实长度,中文字符算2长度*/
  if (typeof (str) !== "string" || str.length === 0)
  return 0;
  let len = 0;
  let strLen = str.length;
  for (let i = 0; i < strLen; i++) {
      let a = str.charAt(i);
      len++;
      if (escape(a).length > 4) {/*中文字符的长度经编码之后大于4*/
          len++;
      }
  }
  return len;
}
export const getSubStr = (str, len, isSuffix) => {
  if (typeof (str) !== "string" || str.length === 0) return "";
  let realLen = getStrRealLen(str);
  if (realLen <= len) {
    return str;
  } else {
    let str_length = 0;
    let str_cut = String();
    let str_len = str.length;
    if (isSuffix)
      len -= 3;
    for (let i = 0; i < str_len; i++) {
      let a = str.charAt(i);
      str_length++;
      if (escape(a).length > 4) {
        /*中文字符的长度经编码之后大于4*/
        str_length++;
      }
      str_cut = str_cut.concat(a);
      if (str_length >= len) {
        if (isSuffix) {
          str_cut = str_cut.concat("...");
        }
        return str_cut;
      }
    }
    /*如果给定字符串小于指定长度，则返回源字符串；*/
    if (str_length < len)
    return str;
  }
}

